/*

* ------------------------------------------------------------------------
* ------------------------------------------------------------------------
* |     Smart-Shop开源商城系统/ Java商城-首选电商平台系统 请务必保留此注释头信息
* |     开源地址: https://gitee.com/ningbo-qishan/gruul-mall
* ------------------------------------------------------------------------
* ------------------------------------------------------------------------
* |     可正常分享和学习源码,未经授权 不可商用！
* |     商业版中台购买/开源版商业授权 联系技术客服
* |     官网:  https://www.73app.cn/
* ------------------------------------------------------------------------
* ------------------------------------------------------------------------
* |     Copyright (c) 2016-2999 宁波启山智软科技有限公司
* |     版权所有 ,侵权必究！
* ------------------------------------------------------------------------

*/
package com.medusa.gruul.payment.handler.merchant;

import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.StrUtil;
import com.egzosn.pay.common.bean.CertStoreType;
import com.egzosn.pay.common.util.sign.SignUtils;
import com.medusa.gruul.common.core.auth.client.helper.ISecurity;
import com.medusa.gruul.common.core.exception.ServiceException;
import com.medusa.gruul.common.core.util.SystemCode;
import com.medusa.gruul.payment.api.entity.MerchantDetails;
import com.medusa.gruul.payment.api.entity.PaymentMerchantConfig;
import com.medusa.gruul.payment.api.enums.PayType;
import com.medusa.gruul.payment.api.model.dto.MerchantDTO;
import com.medusa.gruul.payment.common.annotation.MerchantHandler;
import com.medusa.gruul.payment.common.constant.Snowflakes;
import com.medusa.gruul.payment.conf.PayProperty;
import com.medusa.gruul.payment.service.IMerchantDetailsService;
import com.medusa.gruul.payment.service.IPaymentMerchantConfigService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

/**
 * 支付宝商户处理器
 * @author 张治保
 * date 2022/1/21
 */
@Component
@RequiredArgsConstructor
@MerchantHandler(PayType.ALIPAY)
public class AlipayMerchantHandler extends AbstractMerchantHandler {

    private final IPaymentMerchantConfigService paymentMerchantConfigService;
    private final PayProperty payProperty;
    private final IMerchantDetailsService merchantDetailsService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    protected void handle(MerchantDTO alipayMerchant) {
        if (StrUtil.isBlank(alipayMerchant.getKeyPrivate()) || StrUtil.isBlank(alipayMerchant.getKeyPublic())){
            throw new ServiceException(SystemCode.PARAM_VALID_ERROR);
        }
        String tenantId = ISecurity.tenantId().must();
        MerchantDetails details = paymentMerchantConfigService.paymentMerchant(PayType.ALIPAY,null);
        /*
         * 不存在则新增商户配置详情
         */
        if (details == null) {
            String detailsId = Snowflakes.MERCHANT_ID_SNOWFLAKE.nextIdStr();
            details = new MerchantDetails().setDetailsId(detailsId)
                .setPayType(PayType.ALIPAY)
                .setAppid(alipayMerchant.getAppid())
                .setCertStoreType(CertStoreType.INPUT_STREAM.name())
                .setKeyPrivate(alipayMerchant.getKeyPrivate())
                .setKeyPublic(alipayMerchant.getKeyPublic())
                .setSignType(SignUtils.RSA2.getName())
                .setNotifyUrl(StrUtil.format(payProperty.getNotifyTemplate(), tenantId, detailsId))
                .setReturnUrl(StrUtil.format(payProperty.getReturnTemplate(), tenantId, PayType.ALIPAY.name()))
                .setInputCharset(CharsetUtil.UTF_8)
                .setIsTest(false);

            merchantDetailsService.save(details);
            //保存当前店铺与配置的对应关系
            paymentMerchantConfigService.save(new PaymentMerchantConfig().setDetailsId(detailsId));
            return;
        }
        /*
         * 存在则更新
         */
        details.setAppid(alipayMerchant.getAppid())
            .setKeyPrivate(alipayMerchant.getKeyPrivate())
            .setKeyPublic(alipayMerchant.getKeyPublic());
        merchantDetailsService.updateById(details);
    }
}
